package com.my9yu.common.socket.codec.json;

import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

import com.my9yu.common.socket.util.JsonUtils;

public class JsonEncoder implements ProtocolEncoder {

	public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
		String value = JsonUtils.object2String(message);
		IoBuffer buf = IoBuffer.allocate(value.length());
		buf.putString(value, Charset.forName("UTF-8").newEncoder());
//		buf.put(value.getBytes());
		buf.flip();
		out.write(buf);
	}

	public void dispose(IoSession session) throws Exception {

	}

}
